var initionstate= [];
var _=require('lodash');
function carts(state=initionstate,action){
    switch(action.type){
    case'ADD_TO_CART':
    var pos=_.findIndex(state,{id:action.payload.id});
    if(action.payload.num){
        if(pos!==-1){
            state[pos].quantity+=action.payload.num;
            return [...state];
        }else{
            action.payload.quantity=action.payload.num;
            return [...state,action.payload];
        }
    }else{
        if(pos!==-1){
            state[pos].quantity+=1;
            return [...state];
        }else{
            action.payload.quantity=1;
            return [...state,action.payload];
        }
    }
   case "REMOVE":
        console.log('remove start',action.payload);
        var pos=_.findIndex(state,{id:action.payload.id});
        console.log(pos);
        state.splice(pos,1);

        console.log('remove ok',state);
        return [...state];
    default:
    return state;    
    }
}
export default carts;